package com.plugin.manage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.plugin.core.model.entity.ElderCheckIn;
import com.plugin.manage.pojo.vo.ElderCheckinDetailVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 入住记录Mapper接口
 *
 * @author mark
 * @date 2025-07-31
 */
@Mapper
public interface CheckinMapper extends BaseMapper<ElderCheckIn> {

    /**
     * 统计今日入住人数
     * 统计elder_check_in表中status=4（已缴费入住）的记录数
     * 
     * @param startTime 开始时间（今日00:00:00）
     * @param endTime 结束时间（今日23:59:59）
     * @return 今日入住人数
     */
    @Select("SELECT COUNT(*) FROM elder_check_in WHERE del = 0 AND status = 4 and is_leave = 0 " +
            "AND check_in_date >= #{startTime} AND check_in_date <= #{endTime}")
    Long countTodayCheckedIn(@Param("startTime") LocalDateTime startTime, 
                             @Param("endTime") LocalDateTime endTime);

    /**
     * 统计待审核的入住记录数量（status=1）
     * 
     * @return 待审核的入住记录数量
     */
    @Select("SELECT COUNT(*) FROM elder_check_in WHERE del = 0 AND status = 1 and is_leave = 0")
    Long countPendingReviewCheckins();
    
    /**
     * 统计总的已入住人数（status=4）
     * 
     * @return 总的已入住人数
     */
    @Select("SELECT COUNT(*) FROM elder_check_in WHERE del = 0 AND status = 4 and is_leave = 0")
    Long countTotalCheckedIn();
    
    /**
     * 统计未离开且状态在1-5之间的记录数
     * 
     * @param status 状态值(1-5)
     * @return 符合条件的记录数
     */
    @Select("SELECT COUNT(*) FROM elder_check_in WHERE del = 0 AND is_leave = 0 AND status = #{status}")
    Long countByStatusAndNotLeave(@Param("status") Integer status);

    /**
     * 关联查询长者信息和入住状态，按时间倒序排列
     * 
     * @return 长者状态信息列表
     */
    @Select("SELECT " +
           "ei.elder_id AS elderId, " +
           "ei.realname AS realname, " +
           "ci.room_number AS roomNumber, " +
           "ci.status AS status, " +
           "ci.is_leave AS isLeave, " +
           "ci.check_in_date AS checkInDate " +
           "FROM elder_info ei " +
           "JOIN elder_check_in ci ON ei.elder_id = ci.elder_id " +
           "WHERE ei.del = 0 AND ci.del = 0 AND ci.is_leave = 0 " +
           "ORDER BY ci.check_in_date DESC limit 5")
    List<Map<String, Object>> getElderStatusList();
    
    /**
     * 获取长者入住详情
     * 
     * @return 长者入住详情列表
     */
    @Select("SELECT " +
           "ei.realname, " +
           "ei.age, " +
           "ei.care_level AS careLevel, " +
           "ci.room_number AS roomNumber, " +
           "ci.check_in_amount AS checkInAmount, " +
           "ci.check_in_date AS startDate, " +
           "ci.end_date AS endDate, " +
           "ci.create_time AS createTime, " +
           "ci.deposit_amount AS deposit, " +
           "ci.payment_method AS paymentMethod, " +
           "ci.termination_date AS terminationDate, " +
           "ci.termination_reason AS terminationReason, " +
           "ei.mobile_phone AS contactNumber " +
           "FROM elder_info ei " +
           "JOIN elder_check_in ci ON ei.elder_id = ci.elder_id " +
           "WHERE ei.del = 0 AND ci.del = 0 " +
           "ORDER BY ci.create_time DESC")
    List<ElderCheckinDetailVO> getElderCheckinDetailList();
}
